//
//  FriendTableViewCell.m
//  YSSmartHome
//
//  Created by yanghao on 2017/3/20.
//  Copyright © 2017年 yahaong. All rights reserved.
//

#import "FriendTableViewCell.h"
#import "YSCommon.h"
#import "Friend.h"
#import "DataBaseManager.h"
#import "ZTMessage.h"
#import "IMMessageModel.h"
@interface FriendTableViewCell ()
@property (nullable, nonatomic, strong) UIImageView* mImgIcon;
@property (nullable, nonatomic, strong) UILabel* mLblName;
@property (nullable, nonatomic, strong) UILabel* mLblMessage;
@property (nullable, nonatomic, strong) UIView* mSplitView;
@property (nullable, atomic, strong) IMMessageModel* messageModel;
@property (nullable, nonatomic, strong) UIButton* mUnReadMessageBtn;

@end
@implementation FriendTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    [self initSubviews];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initSubviews];
        self.messageModel = [[IMMessageModel alloc]init];
    }
    return self;
}
#pragma mark - private method
- (void)initSubviews
{
    self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    [self.contentView addSubview:self.mImgIcon];
    [self.contentView addSubview:self.mLblName];
    [self.contentView addSubview:self.mLblMessage];
    [self.contentView addSubview:self.mUnReadMessageBtn];
    [self.contentView addSubview:self.mSplitView];
    [self layoutPageSubviews];
}

- (void)layoutPageSubviews
{
    [self.mImgIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.leading.equalTo(self.contentView.leading).offset(kIphoneSize_Widith(5));
    }];
    [self.mLblName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.mImgIcon.trailing).offset(kIphoneSize_Widith(5));
        make.top.equalTo(self.mImgIcon).offset(kIphoneSize_Height(5));
    }];
    [self.mLblMessage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.mLblName);
        make.top.equalTo(self.mLblName.bottom).offset(kIphoneSize_Height(5));
    }];
    [self.mUnReadMessageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.trailing.equalTo(self.contentView);
    }];
    [self.mSplitView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.equalTo(self.contentView);
        make.height.equalTo(1);
    }];
    
}

#pragma mark - getter and setter 

- (void)setFriendModel:(Friend *)friendModel
{
    _friendModel = friendModel;
    self.mLblName.text = friendModel.mobile;
    [kDataBaseManager loadIMMessageWithApsn:kZTCloudGatewayApsn luid:[XAI mobileToLuid:friendModel.mobile] complete:^(NSArray *array) {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.messageModel.messages = [array mutableCopy];
            if (array.count > 0) {
                ZTMessage* message = array.lastObject;
                self.mLblMessage.text = message.text;
                NSUInteger index = 0;
                for (ZTMessage* message in array) {
                    if (!message.isRead) {
                        index++;
                    }
                }
                self.mUnReadMessageBtn.hidden = index == 0;
                [self.mUnReadMessageBtn setTitle:[NSString stringWithFormat:@"%zd",index] forState:UIControlStateNormal];
            }
            else
            {
                self.mUnReadMessageBtn.hidden = YES;
            }
     
        });
    }];
}
- (UIImageView *)mImgIcon
{
    if (!_mImgIcon) {
        _mImgIcon = [[UIImageView alloc]init];
        _mImgIcon.image = [UIImage imageNamed:@"Avatar_other"];
    }
    return _mImgIcon;

}

- (UILabel *)mLblName
{
    if (!_mLblName) {
        _mLblName = [[UILabel alloc]init];
        _mLblName.text = @"赵敏";
        _mLblName.textColor = RGBColor(51, 51, 51);
    }
    return _mLblName;
}

- (UILabel *)mLblMessage
{
    if (!_mLblMessage) {
        _mLblMessage = [[UILabel alloc]init];
        _mLblMessage.text = @"劝君更尽一杯酒，西出阳关无故人";
        _mLblMessage.textColor = RGBColor(102, 102, 102);
        _mLblMessage.font = [UIFont systemFontOfSize:14];
    }
    return _mLblMessage;

}

- (UIView *)mSplitView
{
    if (!_mSplitView) {
        _mSplitView = [[UIView alloc]init];
        _mSplitView.backgroundColor = RGBColor(225, 225, 225);
    }
    return _mSplitView;
}

- (UIButton *)mUnReadMessageBtn
{
    if (!_mUnReadMessageBtn) {
        _mUnReadMessageBtn = [UIButton buttonWithType:UIButtonTypeCustom];

        [_mUnReadMessageBtn setBackgroundColor:RGBColor(239, 75, 58)];
        _mUnReadMessageBtn.frame = CGRectMake(0, 0, 30, 30);
        _mUnReadMessageBtn.layer.cornerRadius = _mUnReadMessageBtn.frame.size.width * 0.5;
        _mUnReadMessageBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_mUnReadMessageBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    return _mUnReadMessageBtn;
}
@end
